package com.sale.succ.utils;

import java.util.Calendar;
import java.util.Date;

import eve.sys.Convert;

public final class DateFormatter {
	private DateFormatter() {
		//
	}

	/**
	 * 
	 * @param dateStr
	 *            - format DD$MM$YYYY example= 22.02.2003
	 * @return
	 */
	public static Date stringToDate(String dateStr)
			throws NumberFormatException {
		String day = dateStr.substring(0, 2);
		String month = dateStr.substring(3, 5);
		String year = dateStr.substring(6, 10);
		Calendar now = Calendar.getInstance();
		now.set(Calendar.DAY_OF_MONTH, Convert.parseInt(day));
		now.set(Calendar.MONTH, Convert.parseInt(month) - 1);
		now.set(Calendar.YEAR, Convert.parseInt(year));
		return now.getTime();
	}

	/**
	 * 
	 * @param date
	 * @param symb
	 * @return string - format DD$MM$YYYY,example= 22.02.2003
	 */
	public static String dateToString(Date date, String symb) {
		Calendar now = Calendar.getInstance();
		now.setTime(date);
		String day = Convert.toString(now.get(Calendar.DAY_OF_MONTH));
		if (day.length() == 1) {
			day = "0" + day;
		}
		String month = Convert.toString(now.get(Calendar.MONTH) + 1);
		if (month.length() == 1) {
			month = "0" + month;
		}
		String year = Convert.toString(now.get(Calendar.YEAR));

		return day + symb + month + symb + year;
	}
	
	/**
	 * 
	 * @param date
	 * @param symb
	 * @return string - format DD$MM$YYYY,example= 22.02.2003(1:2:3:4)
	 */
	public static String dateTimeToString(Date date, String symb) {
		Calendar now = Calendar.getInstance();
		now.setTime(date);
		String day = Convert.toString(now.get(Calendar.DAY_OF_MONTH));
		if (day.length() == 1) {
			day = "0" + day;
		}
		String month = Convert.toString(now.get(Calendar.MONTH) + 1);
		if (month.length() == 1) {
			month = "0" + month;
		}
		String year = Convert.toString(now.get(Calendar.YEAR));
		
		String h = Convert.toString(now.get(Calendar.HOUR));
		String m = Convert.toString(now.get(Calendar.MINUTE));
		String s = Convert.toString(now.get(Calendar.SECOND));
		String ms = Convert.toString(now.get(Calendar.MILLISECOND));

		return day + symb + month + symb + year + "_" + h + symb + m + symb + s + symb + ms;
	}
}
